This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
#setup
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(ggplot2)
library(tibble)
library(plotly)
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
set.seed(1) # For reproducible random numbers
# Buyers
how_many_buyers <- 100
buyers <- tibble(id = 1:how_many_buyers,
reserve = sample(c(200:1000), size = how_many_buyers,
replace = TRUE))
# Bar chart
a <- ggplot(buyers, aes(x=id, y=reserve)) +
geom_bar(stat = "identity")
#Ordered bar chart
a <- ggplot(buyers, aes(x=reorder(id, -reserve), y=reserve)) +
geom_bar(stat = "identity")
# Lollypop
ggplot(buyers, aes(x=id, y=reserve)) +
geom_point() +
geom_segment( aes(x=id, xend=id, y=0, yend=reserve))
# Ordered lollipop
ggplot(buyers, aes(x=reorder(id, -reserve), y=reserve)) +
geom_point() +
geom_segment( aes(x=reorder(id, -reserve), xend=reorder(id, -reserve),
y=0, yend=reserve)) +
scale_y_continuous(breaks = seq(0, 1000, by=200), limits=c(0,1000)) +
labs(x = "Buyer ID", y = "Reserve price ($/month)")
# Cumulative demand
buyers <- buyers %>%
arrange(desc(reserve)) %>%
mutate(cumulative_demand = 1:how_many_buyers)
# Graph
p <- ggplot(buyers, aes(x=cumulative_demand, y=reserve)) +
geom_point() +
geom_segment( aes(x=cumulative_demand, xend=cumulative_demand,
y=0, yend=reserve)) +
scale_y_continuous(breaks = seq(0, 1000, by=200), limits=c(0,1000)) +
labs(x = "Quantity", y = "Reserve price ($/month)")
# Interactive graph
q <- ggplotly(p) %>% config(displayModeBar = F, displaylogo = F)
q
You can also embed plots, for example:
Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.